<%@page import="java.nio.channels.SeekableByteChannel"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

<script type="text/javascript"
	src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
	/* header("Content-Type: text/html; charset=utf-8"); */
	/* 모달 스크립트 */
	$(document).ready(function() {
		$('.login-window').click(function() {
			var loginBox = $(this).attr('href');
			$(loginBox).fadeIn(300);
			var popMargTop = ($(loginBox).height() + 24) / 2;
			var popMargLeft = ($(loginBox).width() + 24) / 2;
			$(loginBox).css({
				'margin-top' : -popMargTop,
				'margin-left' : -popMargLeft
			});
			$('body').append('<div id="mask"></div>');
			$('#mask').fadeIn(300);
			return false;
		});
		$('a.close, #mask').live('click', function() {
			$('#mask , .login-popup').fadeOut(300, function() {
				$('#mask').remove();
			});
			return false;
		});
	});
	/* 모달 끝 */

	var name;
	function fn_onkeypress() {
		name = $("#inputtag").val();
		$("#namelist").show();
		var namelist;
		$
				.ajax({
					url : "namecheck.do",
					data : "name=" + name,
					success : function(data) {
						console.log(data.list.length);
						$("#namelist").empty();
						if (typeof namelist == "undefined") {
							namelist = "";
						}
						for (var k = 0; k < data.list.length; k++) {
							namelist = "<div class='patient_name' name='patient_name'><input type='text' class='pname' name='pname' value='"
									+ data.list[k].name
									+ "' readonly='readonly' onclick='idxcheck(this);'></div>";
							$("#namelist").append(namelist);
						}
					},
					error : function(data) {
						//alert("fail");
					}
				});
	}

	$(document)
			.ready(
					function() {
						$("#inputtag").focus();
						$(".mouse")
								.hover(
										function() {
											var idx = $(".mouse").index(this);
											$(".mouse").eq(idx).css(
													"background-color",
													"silver");
											$(this).find('input').css(
													"background-color",
													"silver");
											$(this).find('input').css("border",
													" silver");
											$(this).find('input').css(
													"padding", "1px");

										},
										function() {
											var idx = $(".mouse").index(this);
											$(".mouse").eq(idx)
													.css("background-color",
															"white");
											$(this).find('input')
													.css("background-color",
															"white");
											$(this).find('input').css("border",
													"white");
											$(this).find('input').css(
													"padding", "1px");
										});

						idxcheck = function(aaaa) {
							var idx = $('.pname').index(aaaa);
							$("#inputtag").val($('.pname').eq(idx).val());
							$("#namelist").hide();
						}

						//환자 검색 버튼
						$("#btncheckname")
								.click(
										function() {
											var name = $("#inputtag").val();
											$
													.ajax({
														url : "namecheck.do",
														data : "name=" + name,
														success : function(data) {
															var name = data.name;
															var jumin1 = data.jumin1;
															var patient_id = data.patient_id;
															$('#sidetable')
																	.empty();
															var sidetable = "<table id='sidetable'><tr><td>이름</td><td>주민번호</td></tr></table>";
															$('#sidejson')
																	.html(
																			sidetable);
															for (var k = 0; k < data.list.length; k++) {
																var html = "<tr id='nameTr' class='nameTr' ondblclick='dbclick(this);'>"
																		+ "<td><input type='text' class='selectname' readonly='readonly' value='"+data.list[k].name+"'>"
																		+ "</td><td><input type='text' class='selectjumin1' readonly='readonly' value='"+data.list[k].jumin1+"'></td></tr>"
																		+ "<input type='hidden' id='patient_id' class='patient_id' name='patient_id' value='"+data.list[k].patient_id+"' >";
																$('#sidetable')
																		.append(
																				html);
															}

														},
														error : function(data) {
															//alert("fail");
														}
													});
										});

					});
	function fn_show(aaa) {
		idx = $(".tdshow").index(aaa);
		cheobang_id = $("td[class='cheobang_id']").eq(idx).text();
		$
				.ajax({
					url : "cheobangdata.do",
					data : "cheobang_id=" + cheobang_id,
					success : function(data) {
						$("#righttable3").empty();
						var htmlobj2 = "<tr>" + "<td colspan='2'>복용법</td>"
								+ "</tr>";
						$("#righttable3").html(htmlobj2);
						for (var i = 0; i < data.doselist.length; i++) {
							var html4 = "<tr><td>" + data.doselist[i].dose_id
									+ "</td>" + "<td>"
									+ data.doselist[i].dose_name + "</td>"
									+ "</tr>"
									+ "<tr><td colspan='2'>설명</td></tr>"
									+ "<tr><td colspan='2'>"
									+ data.doselist[i].dose_explain
									+ "</td></tr>";
							$("#righttable3").append(html4);
						}
						;
						$("#righttable").empty();
						var htmlobj = "<tr>"
								+ "<td></td><td>약이름</td><td>가격</td><td>수량</td><td>내/외복</td>"
								+ "<td>단위</td></tr>";
						$("#righttable").html(htmlobj);
						for (var i = 0; i < data.list.length; i++) {
							var html3 = "<tr><td>" + data.list[i].medicine_id
									+ "</td>" + "<td>" + data.list[i].name
									+ "</td>" + "<td>" + data.list[i].fee
									+ "</td>" + "<td>" + data.list[i].quantity
									+ "</td>" + "<td>" + data.list[i].explain
									+ "</td>" + "<td>" + data.list[i].unit
									+ "</td></tr>";
							$("#righttable").append(html3);
						}
						;
						$("#cheobangn").empty();
						var html2 = "<tr>" + "<th>처방 완료 번호</th>" + "<td>"
								+ data.cheobanglist.cheobang_id + "</td>"
								+ "<th>접수번호</th>" + "<td>"
								+ data.cheobanglist.jupsoo_ID + "</td>"
								+ "</tr>" + "<tr>" + "<th>접수날짜</th>" + "<td>"
								+ data.cheobanglist.jupsoo_DATE + "</td>"
								+ "<th>접수시간</th>" + "<td>"
								+ data.cheobanglist.jupsoo_TIME + "</td>"
								+ "</tr>" + "<tr>" + "<th>초재진여부</th>" + "<td>"
								+ data.cheobanglist.is_WARD + "</td>"
								+ "<th>공휴일여부</th>" + "<td>"
								+ data.cheobanglist.is_HOLIDAY + "</td>"
								+ "</tr>" + "<tr>" + "<th>야간여부</th>" + "<td>"
								+ data.cheobanglist.is_NIGHT + "</td>"
								+ "<th>보험타입</th>" + "<td>"
								+ data.cheobanglist.type + "</td>" + "</tr>"
								+ "<tr>" + "<th>외래/입원 여부</th>" + "<td>"
								+ data.cheobanglist.jupsoo_CODE_ID + "</td>"
								+ "<th>첨부파일</th>" + "<td>"
								+ data.cheobanglist.addfile + "</td>" + "</tr>"
								+ "<tr>" + "<th>내원자번호</th>" + "<td>"
								+ data.cheobanglist.visitant_ID + "</td>"
								+ "<th>환자번호</th>" + "<td>"
								+ data.cheobanglist.patient_ID + "</td>"
								+ "</tr>" + "<tr>" + "<th>대분류</th>" + "<td>"
								+ data.cheobanglist.habitat_ID + "</td>"
								+ "<th>소분류</th>" + "<td>"
								+ data.cheobanglist.content + "</td>" + "</tr>"
								+ "<tr>" + "<th>상세분류</th>" + "<td>"
								+ data.cheobanglist.habitat_DETAIL_ID + "</td>"
								+ "<th>검사번호</th>" + "<td>"
								+ data.cheobanglist.geomsa_CODE_ID + "</td>"
								+ "</tr>" + "<tr>" + "<th>의사 고유번호</th>"
								+ "<td>" + data.cheobanglist.doctor_ID
								+ "</td>" + "<th>담당 의사</th>" + "<td>"
								+ data.cheobanglist.doctor_NAME + "</td>"
								+ "</tr>" + "<tr>" + "<th>부서번호</th>" + "<td>"
								+ data.cheobanglist.dept_ID + "</td>"
								+ "<th>부서이름</th>" + "<td>"
								+ data.cheobanglist.dept_NAME + "</td>"
								+ "</tr>";
						$("#cheobangn").append(html2);
						for (var d = 0; d < data.cheobanglist.length; d++) {
							var disease = "<tr>" + "<th>병명코드</th>" + "<td>"
									+ data.cheobanglist[d].disease_ID + "</td>"
									+ "<th>병명</th>" + "<td>"
									+ data.cheobanglist[d].disease_NAME
									+ "</td>" + "</tr>";
							$(".patiend_table").append(disease)
						}
						var opinion = "<tr><th>의사소견</th><td colspan='3'><textarea rows='3' cols='45' readonly style='border:0px; font-size:8pt;'>"
								+ data.ch[0].opinion + "</textarea></td></tr>"
						$(".patiend_table").append(opinion);
					}
				});
		$(".cheobang_hide").show();
	}
	function fn_close() {
		$(".cheobang_hide").hide();
	}

	var patient_id;

	//더블클릭
	function dbclick(aaaa) {
		//alert("더블클릭");
		var idx = "";
		idx = $(".nameTr").index(aaaa);
		var name = $('.selectname').eq(idx).val();
		var jumin1 = $('.selectjumin1').eq(idx).val();
		patient_id = $('.patient_id').eq(idx).val();

		$
				.ajax({

					url : 'showinfo.do',
					data : 'name=' + name + '&jumin1=' + jumin1
							+ '&patient_id=' + patient_id,
					dataType : 'json',
					success : function(data) {

						var html = "<td id='p1'>"
								+ patient_id
								+ "</td>"
								+ "<td id='p2'>"
								+ data.patient.name
								+ "</td>"
								+ "<td id='p3'>"
								+ data.patient.gender
								+ "</td>"
								+ "<td id='p4'>"
								+ data.patient.jumin1
								+ "-"
								+ data.patient.jumin2
								+ "</td>"
								+ "<td id='p5'>"
								+ data.patient.is_kuk
								+ "</td>"
								+ "<td id='p6'>"
								+ data.patient.is_car
								+ "</td>"
								+ "<td id='p7'>"
								+ data.patient.is_san
								+ "</td>"
								+ "<td id='p8'>"
								+ data.patient.zipcode1
								+ "</td>"
								+ "<td id='p9'>"
								+ data.patient.zipcode2
								+ "</td>"
								+ "<td id='p10'>"
								+ data.patient.addr1
								+ "</td>"
								+ "<td id='p11'>"
								+ data.patient.addr2
								+ "</td>"
								+ "<td id='p12'>"
								+ data.patient.tel
								+ "</td>"
								+ "<input type='hidden' name='patient_id' value='"+patient_id+"'> ";
						$("#pinfo").html(html);
						$(".patiend_table_1").empty();
						$(".patiend_table_1")
								.html(
										"<tr><td>날짜</td><td>진료과</td><td>담당의사</td><td>처방번호</td><td>입원 여부</td><td>상세보기</td></tr>");
						for (var i = 0; i < data.list.length; i++) {
							var html2 = "<tr class='cheobangdata'><td>"
									+ data.list[i].jupsoo_DATE
									+ "</td>"
									+ "<td>"
									+ data.list[i].dept_NAME
									+ "</td>"
									+ "<td>"
									+ data.list[i].doctor_NAME
									+ "</td>"
									+ "<td class='cheobang_id'>"
									+ data.list[i].cheobang_id
									+ "</a></td>"
									+ "<td>"
									+ data.list[i].is_WARD
									+ "</td>"
									+ "<td><input type='button' value='보기' class='tdshow' onclick='fn_show(this)'></td></tr>";
							$(".patiend_table_1").append(html2);
						}
					}

				});
		$
				.ajax({
					url : 'showsoonapinfo2.do',
					data : 'patient_id=' + patient_id,
					dataType : 'json',
					success : function(data) {
						$("#soonap_Table")
								.html(
										"<tr><td>접수 일자</td><td>총금액</td><td>진료과</td><td>처방번호</td></tr>");
						for (var a = 0; a < data.soonap.length; a++) {
							var nosoonap = "";
							nosoonap += "<tr><td>" + data.soonap[a].jupsoo_date
									+ "</td>";
							nosoonap += "<td>" + data.soonap[a].total_sg_price
									+ "</td>";
							nosoonap += "<td>" + data.soonap[a].dept_name
									+ "</td>";
							nosoonap += "<td>" + data.soonap[a].cheobang_id
									+ "</td></tr>";
							$("#soonap_Table").append(nosoonap);
						}
					}
				});
		$
				.ajax({

					url : 'showsoonapinfo.do',
					data : 'patient_id=' + patient_id,
					dataType : 'json',
					success : function(data) {
						if (data.total === null) {
							$("#nosoonap_Table")
									.html(
											"<tr><td>접수 일자</td><td>총금액</td><td>미수납 금액</td><td>진료과</td><td>처방번호</td></tr>");
							$(".jupsoohref").show();
						} else {

							$(".jupsoohref").hide();
							$("#nosoonap_Table")
									.html(
											"<tr><td>접수 일자</td><td>총금액</td><td>미수납 금액</td><td>진료과</td><td>처방번호</td></tr>");
							for (var a = 0; a < data.nosoonap.length; a++) {
								var nosoonap = "";
								nosoonap += "<tr><td>"
										+ data.nosoonap[a].jupsoo_date
										+ "</td>";
								nosoonap += "<td>"
										+ data.nosoonap[a].total_sg_price
										+ "</td>";
								nosoonap += "<td>"
										+ data.nosoonap[a].remainder_price
										+ "</td>";
								nosoonap += "<td>" + data.nosoonap[a].dept_name
										+ "</td>";
								if(data.nosoonap[a].cheobang_id === null){
									nosoonap += "<td class='tdshow'>입원</td></tr>"
								}else{
								nosoonap += "<td class='tdshow'>"
										+ data.nosoonap[a].cheobang_id
										+ "</td></tr>";
								}
								$("#nosoonap_Table").append(nosoonap);
							}
							alert("미수납금액이 존재합니다.");
							$("#nosoonap_Table").append(
									"<tr id='total'><td colspan='2'>미수납 총금액</td><td colspan='3'>"
											+ data.total + "</td></tr>");
						}
					}
				});
		$('#sidejson').empty();
		var plist = "<table id='sidetable'><tr><td>이름</td><td>주민번호</td></tr>";
		plist += "<c:forEach items='${list}' var='n'><tr class='mouse'>";
		plist += "<td><input type='text' class='selectname' readonly='readonly' value='${n.name} '></td>"
		plist += "<td><input type='text' class='selectname' readonly='readonly' value='${n.jumin1 }'></td></tr></c:forEach></table>";
		$('#sidejson').html(plist);

	};

	$("#btnExcel").live("click", function() {
		/* var a = document.createElement('a');
		  a = URLEncoder.encode(a, "UTF-8");
		var data_type = 'data:application/vnd.ms-excel';
		var table_html = encodeURIComponent($("#cheobangn").html());
		a.href = data_type + ', ' + table_html;
		a.download = '${patient.name}[${cheobangall.JUPSOO_DATE}].xls';

		a.click();
		e.preventDefault(); */
		window.location.href='cheobangjeon.do?cheobang_id='+cheobang_id;
		
	});
</script>


</head>
<body>
	<div id='namelist' style="display: none;"></div>
	<div id="systembase">
		<!-- 환자 정보 시작 -->
		<div id="systempatient">
			<h3 id="systemmenufont">환자정보</h3>

			<table class="patiend_info_table">
				<tr>
					<td>고유번호</td>
					<td>이름</td>
					<td>성별</td>
					<td>주민번호</td>
					<td>국민보험</td>
					<td>자동차보험</td>
					<td>산업재해보험</td>
					<td>우편번호</td>
					<td>우편번호2</td>
					<td>기본주소</td>
					<td>상세주소</td>
					<td>연락처</td>
				</tr>
				<tr id="pinfo">
				</tr>
			</table>
		</div>
		<!-- 환자 정보 끝 -->

		<div id="systemcontent">
			<!-- 여긴 최근 환자 진료 정보 시작 -->
			<div id="systemcontentleft">
				<h3 id="systemmenufont">최근 진료 정보</h3>
				<table class="patiend_table_1">
					<tr>
						<td>날짜</td>
						<td>진료과</td>
						<td>담당의사</td>
						<td>병명코드</td>
						<td>병명</td>
						<td>처방번호</td>
						<td>입원 여부</td>
					</tr>

				</table>


			</div>
			<!-- 여긴 최근 환자 진료 정보 끝 -->


			<!-- 여긴 수납 정보 시작 -->
			<div id="systemcontentright">
				<h3 id="systemmenufont">수납 정보</h3>
				<table class="patiend_table" id="soonap_Table">
					<tr>
						<td>수납 일자</td>
						<td>수납 금액</td>
						<td>진료과</td>
						<td>처방번호</td>
					</tr>
				</table>
			</div>
			<!-- 여긴 수납 정보 끝 -->

			<!-- 미수납 정보 시작 -->
			<div id="systemcontentright">
				<h3 id="systemmenufont">미수납 정보</h3>
				<table class="patiend_table" id="nosoonap_Table">
					<tr>
						<td>수납 일자</td>
						<td>수납 금액</td>
						<td>진료과</td>
						<td>처방번호</td>
					</tr>
				</table>


			</div>
			<!-- 미수납 정보 끝 -->

			<div id="footbutton">
				<div id="footerbuttondiv" style="float: right; margin-right: 70px;">
					<div id="foot2" style="float: right"></div>

					<a href="#login-box" id="blacklink" class="login-window">등록 하기</a>&nbsp;&nbsp;
					<div class="jupsoohref" style="float: right">
						<a id="blacklink" href="jupsoo.do">접수 하기</a>&nbsp;&nbsp; <a
							id="blacklink" href="office_reser.do">예약 접수 하기</a>
					</div>
				</div>
			</div>
			<div id="login-box" class="login-popup" style="clear: both;">
				<form id="Join" action="patientinfojoin.do" method="post">
					<input type="hidden" id="kuk_bohum" name="is_kuk" value="Y">
					<input type="hidden" id="car_bohum" name="is_car" value="N">
					<input type="hidden" id="san_bohum" name="is_san" value="N">
					<input type="hidden" id="" name="enabled" value="1">
					<div id="Join_box">
						<span id="lbl_name">이름</span> <input type="text" id="name"
							name="name" maxlength="20" placeholder="이름을 입력하세요">
					</div>
					<div id="Join_box">
						<span id="lbl_nation">국적확인</span> <span id="nation_box">내국인</span><input
							type="radio" id="nation" name="nation_code_id" value="NATION01"
							checked="checked"> <span id="nation_box">외국인</span><input
							type="radio" id="nation" name="nation_code_id" value="NATION02">
					</div>
					<div id="Join_box">
						<span id="lbl_jumin">주민등록번호</span> <input type="text" id="jumin1"
							name="jumin1" maxlength="6" placeholder="주민번호 앞 6자리"
							required="required" pattern="^[0-9]{6}$" onkeyPress="if ((event.keyCode<48) || (event.keyCode>57)) event.returnValue=false;"> - <input
							type="password" id="jumin2" name="jumin2" maxlength="7"
							placeholder="주민번호 뒤 7자리" pattern="^[0-9]{7}$" required="required" onkeyPress="if ((event.keyCode<48) || (event.keyCode>57)) event.returnValue=false;">
						<font id="nation_info">※외국인의 경우 외국인등록번호를 입력해주세요</font>
					</div>
					<div id="Join_box">
						<span id="lbl"> 성별 </span>
						<spa id="gender_box"> 남 </span> <input type="radio" id="gender"
							name="gender" value="M" checked="checked"> <span
							id="gender_box"> 여 </span> <input type="radio" id="gender"
							name="gender" value="F"> </span>
					</div>
					<div id="Join_box">
						<span id="lbl_zipcode"> 우편번호 </span> <input type="text"
							id="zipcode1" name="zipcode1" maxlength="20" placeholder="우편번호1"
							readonly="readonly"> - <input type="text" id="zipcode2"
							name="zipcode2" maxlength="20" placeholder="우편번호2"
							readonly="readonly"> <input type="button" id="search"
							value="검색" onclick="openDaumPostcode()" width="10%">
					</div>
					<div id="Join_box">
						<span id="lbl_addr">주소</span><br> <input type="text"
							id="addr1" name="addr1" maxlength="25" placeholder="기본주소"
							readonly="readonly"> <input type="text" id="addr2"
							name="addr2" maxlength="25" placeholder="상세주소">
					</div>
					<div id="Join_box">
						<span id="lbl_tel">연락처</span> <input type="text" id="tel"
							name="tel" maxlength="12" placeholder="'-'없이 입력해주세요"
							pattern="^[0-9]+$">
					</div>
					<div id="Join_box">
						<button type="submit" id="Join_box">환자 등록하기</button>
					</div>
				</form>
				<script src="http://dmaps.daum.net/map_js_init/postcode.js"></script>
				<script>
					//우편번호 검색 팝업
					function openDaumPostcode() {
						new daum.Postcode(
								{
									oncomplete : function(data) {
										// 팝업에서 검색결과 항목을 클릭했을때 실행할 코드를 작성하는 부분.
										// 우편번호와 주소 정보를 해당 필드에 넣고, 커서를 상세주소 필드로 이동한다.
										document.getElementById('zipcode1').value = data.postcode1;
										document.getElementById('zipcode2').value = data.postcode2;
										document.getElementById('addr1').value = data.address;
										//전체 주소에서 연결 번지 및 ()로 묶여 있는 부가정보를 제거하고자 할 경우,
										//아래와 같은 정규식을 사용해도 된다. 정규식은 개발자의 목적에 맞게 수정해서 사용 가능하다.
										//var addr = data.address.replace(/(\s|^)\(.+\)$|\S+~\S+/g, '');
										//document.getElementById('addr').value = addr;

										document.getElementById('addr2')
												.focus();
									}
								}).open();
					}

					//내국인, 외국인 체크박스 선택 하나만 되게끔 해주는 부분
					function check(checknum) {
						var chkbox = document.getElementsByName("nation");

						for (var i = 0; i < chkbox.length; i++) {
							if (chkbox[i] != checknum) {
								chkbox[i].checked = false;
							}
						}
					}
				</script>
			</div>
		</div>
	</div>
	<!-- </div> -->
	<!-- 오른쪽 환자 대기 리스트 시작 -->
	<div id="systembaseside">
		<form id="name" name="name">

			<input id="inputtag" type="text" name="name2" placeholder="환자 이름 검색"
				onkeyup="fn_onkeypress();"> <input type="button"
				id="btncheckname" name="btncheckname" value="검색">
			<hr>
			<div id='sidejson'>
				<table id="sidetable">
					<tr>
						<td>이름</td>
						<td>주민번호</td>
					</tr>
					<c:forEach items="${list}" var="n">
						<tr class='mouse'>
							<td><input type='text' class='selectname'
								readonly='readonly' value='${n.name}'></td>
							<td><input type='text' class='selectname'
								readonly='readonly' value='${n.jumin1 }'></td>
						</tr>
					</c:forEach>
				</table>
			</div>
		</form>

	</div>
	<!-- 오른쪽 환자 대기 리스트 끝 -->
	<div class="cheobang_hide" style="display: none;">
		<div id="systemcontentleft">
			<h3 id="systemmenufont">처방 받은 내역</h3>
			<table class="patiend_table" id="cheobangn">
				<tr id="cheobanginfo">
				</tr>
			</table>
		</div>
		<!-- 여긴 최근 환자 진료 정보 끝 -->

		<!-- 오른쪽 위 시작-->
		<div id="systemcontentright">
			<h3 id="systemmenufont">약 처방 정보</h3>
			<table id="righttable">

			</table>
		</div>
		<!-- 오른쪽 위 끝 -->

		<!-- 오른쪽 중간 -->
		<div id="systemcontentright3">
			<h3 id="systemmenufont">복용법</h3>
			<table id="righttable3">

			</table>
		</div>
		<img alt="" class="printBtn"
			style="width: 30px; height: 30px; margin-top: 0px;"
			src="resources/images/officeword[black].png" id="btnExcel"> 
			<input type="button" value="닫기" style="width: 60px;" onclick="fn_close()">
	</div>
</body>
</html>